package com.xhg.mqtt.listener;

import java.util.LinkedList;
import java.util.Map;

/**
  * 消息返回管理
  * @Description
  * @author E.T
  * @date 2018-07-23 11:02
  * @updateby
  * @updatedate
  * @version 1.0
  * @since 1.0
  */
public class MessageManager {
    private static LinkedList<MessageListener> list = new LinkedList<>();

     /**
      * 添加监听器
      * @param listener
      */
    public static void addListener(MessageListener listener) {
        list.add(listener);
    }

     /**
      * 移除监听器
      * @param listener
      */
    public static void removeListener(MessageListener listener) {
        list.remove(listener);
    }

     /**
      * 监听消息回传消息
      */
    public static void messageArrived(byte[] message){
        for (MessageListener listener : list) {
            listener.messageArrived(message);
        }
    }

     /**
      * 监听重发失败消息
      */
     public static String failMessage(Map<String, MessageEvent> failMsgMap){
         for (MessageListener listener : list) {
              return  listener.failMessage(failMsgMap);
         }
         return null;
     }

  /**
   *连接成功回调
   */
  public static void connectComplete(boolean isConnect){
      if (!list.isEmpty()) {
            list.getLast().connectComplete(isConnect);
        }
  }

  /**
   *重连成功回调
   */
  public static void reconnect(boolean isConnect){
      if (!list.isEmpty()) {
          list.getLast().reconnect(isConnect);
      }
  }
 }
